<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 12:26
 */

/**
 * Class Involved_CartModel
 */
class Involved_CartModel extends BaseModel{

    protected $table = 'cart';


    /**
     * 获取购物车列表
     * @param $user_id
     * @return mixed
     */
    public function GetUserCartList($user_id,$paramObj)
    {
        $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;
        return  DB::table($this->table)
                ->select('cart.cart_id','cart.supplier_id','cart.supplier_name','cart.product_id','cart.product_name','cart.product_img','cart.spec_name','cart.price','cart.number','product.stock')
                ->leftjoin('product','product.product_id', '=', 'cart.product_id')
                ->where('user_id',$user_id)
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->orderBy('cart.created','desc')
                ->get();
    }

    /**
     * PC获取购物车列表
     * @param $user_id
     * @return mixed
     */
    public function GetShopUserCartList($user_id)
    {
        return  DB::table($this->table)
            ->select('cart.cart_id','cart.supplier_id','cart.supplier_name','cart.product_id','cart.unit','cart.product_name','cart.product_img','cart.spec_name','cart.price','cart.number','product.stock')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->where('user_id',$user_id)
            ->orderBy('cart.created','desc')
            ->get();
    }



    /**
     * PC获取购物车商品个数
     * @param $user_id
     * @return mixed
     */
    public function ShopCartCount($user_id)
    {
        return  DB::table($this->table)
            ->select('cart.cart_id')
            ->where('user_id',$user_id)
            ->count();
    }


    /**
     * 获取购物车详情
     * @return mixed
     */
    public function GetCartDetail($cart_id)
    {
        return  DB::table($this->table)->where('cart_id',$cart_id)->first();
    }


    /**
     * 删除购物车
     * @param $user_id
     * @param $cart_id
     * @return mixed
     */
    public function DeleteCart($user_id,$cart_id)
    {
        return  DB::table($this->table)
            ->where('cart_id', $cart_id)
            ->where("user_id", $user_id)
            ->delete();
    }

    /**
     * 查询是否存在商品
     * @param $product_id
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-2-21
     */
    public function GetProductId($product_id, $user_id)
    {
        return DB::table($this->table)
            ->where(['product_id'=>$product_id, 'user_id' => $user_id])
            ->first();
    }

    public function GetCartList($product_id,$user_id)
    {
        return DB::table($this->table)
                ->select('supplier_id','supplier_name','product_id','goods_thumb','early_pay','insurance','is_early_pay','is_insurance')
                ->whereIn('product_id',$product_id)
                ->where('user_id',$user_id)
                ->get();
    }

    /**
     * 购物车信息
     * @param $cart_id
     * @return mixed
     * @author alice
     * @date 2016-2-23
     */
    public function GetCartInfo($cart_id)
    {
        return DB::table($this->table)
                ->select('product_id','supplier_id')
                ->whereIn('cart_id',$cart_id)
                ->get();
    }

    /**
     * 确认订单商品
     * @param $user_id
     * @return mixed
     */
    public function GetCartProducts($user_id,$cart_id)
    {
        $cart_products =  DB::table($this->table)
            ->select('supplier_id','supplier_name','product_id','product_name','product_img','price','number','spec_name')
            ->where(['user_id'=>$user_id,'cart_id'=>$cart_id])
            ->first();
        return $cart_products;
    }

    public function GetCartAmount($user_id,$supplier_id)
    {
       return DB::table($this->table)
            ->where(['user_id' => $user_id, 'supplier_id' => $supplier_id ])
            ->sum('amount');
    }


    /**
     * 购物车信息
     * @param $user_id
     * @return mixed
     * @date 2016-7-02
     */
    public function GetShopCartId($user_id)
    {
        return DB::table($this->table)
            ->select('cart_id')
            ->where('user_id',$user_id)
            ->get();
    }


    /**
     * 删除购物车
     * @param $cart_id
     * @return mixed
     */
    public function ShopCartDeleteAll($cart_id)
    {
        return  DB::table($this->table)
            ->whereIn('cart_id', $cart_id)
            ->delete();
    }




}